package org.example;

import org.example.Enum.ActionEnum;
import org.example.Enum.CisternState;
import org.example.Enum.DispenserState;
import org.example.Event.TurnOffEvent;
import org.example.Event.TurnOnEvent;

public class Main {
    public static void main(String[] args) {
        StateMachine stateMachine = machineConfig();
        // 当前状态为关闭，
//        System.out.println("currentState: " + stateMachine.getCurrentState());
        stateMachine.execute(ActionEnum.time_at_seven_morning, CisternState.HAVE_WATER);
        System.out.println("currentState: " + stateMachine.getCurrentState());
        stateMachine.execute(ActionEnum.arrive_max,null);
    }

    protected static StateMachine machineConfig(){
        // 配置状态机
        StateMachine stateMachine = new StateMachine(DispenserState.TURN_OFF);  // 初始状态为休眠
        // 初始状态为关闭条件下，水温达到最低，水箱有水，开始烧水
        stateMachine.addStateTranslation().source(DispenserState.TURN_OFF)
                .when(ActionEnum.arrive_minimum).Condition(CisternState.HAVE_WATER)
                .targets(DispenserState.TURN_ON).how(new TurnOnEvent());
        // 初始状态为开启条件下，水温达到最高，关闭电源
        stateMachine.addStateTranslation().source(DispenserState.TURN_ON)
                        .when(ActionEnum.arrive_max).Condition(null)
                        .targets(DispenserState.TURN_OFF).how(new TurnOffEvent());
        // 初始状态为关闭条件下，时间到达早上七点，水箱有水，开始烧水
        stateMachine.addStateTranslation().source(DispenserState.TURN_OFF)
                .when(ActionEnum.time_at_seven_morning).Condition(CisternState.HAVE_WATER)
                .targets(DispenserState.TURN_ON).how(new TurnOnEvent());
        // 初始状态为开启条件下，时间到达晚上十一点，关闭电源
        stateMachine.addStateTranslation().source(DispenserState.TURN_ON)
                .when(ActionEnum.time_at_eleven_evening).Condition(null)
                .targets(DispenserState.TURN_OFF).how(new TurnOffEvent());
        return stateMachine;
    }
}